package com.zhuss.vent.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.vent.vent.DeleteVentAppendBo;
import com.zhuss.bean.bo.vent.vent.wechat.CollectVentBo;
import com.zhuss.bean.vo.vent.vent.DetailVentAppendVo;
import com.zhuss.bean.vo.vent.vent.DetailVentVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 取消收藏匿名吐槽 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("ventDelCollect")
public class VentDelCollect extends AbstractProcessor<Object, CollectVentBo> implements Processor<Object, CollectVentBo> {

    @Autowired
	@Qualifier("ventRead")
	private VentRead ventRead;
    
    @Autowired
	@Qualifier("ventAppendRead")
	private VentAppendRead ventAppendRead;
    
    @Autowired
    @Qualifier("ventAppendDelete")
    private Processor<Object, DeleteVentAppendBo> ventAppendDelete;
    
    /**
     * 校验
     */
    @Override
    public void check(CollectVentBo bo) throws WpsException {
    	Long ventId = bo.getVentId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailVentAppendVo result = ventAppendRead.getByType(appendType, ventId, bo.getConcurrentUserId());
    	
    	if (result == null) {
    		throw new WpsException(ExceptionCode.匿名吐槽未收藏);
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(CollectVentBo param) throws WpsException {
    	Long ventId = param.getVentId();
    	Integer appendType = param.getAppendType();
    	
    	DetailVentAppendVo result = ventAppendRead.getByType(appendType, ventId, param.getConcurrentUserId());
    	
    	DetailVentVo vent = ((DetailVentVo) ventRead.detail(ventId));
    	String name = vent.getTitle();
    	
    	DeleteVentAppendBo bo = new DeleteVentAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	bo.setVentAppendId(result.getVentAppendId());
    	ventAppendDelete.process(bo);
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CollectVentBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.匿名吐槽.key());
    	record.setAction(LogActionEnum.匿名吐槽取消收藏.key());
    	record.setTargetId(param.getVentId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
